﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using SqlSugar;

namespace Magic.Core;
/// <summary>
/// SqlSugar服务
/// </summary>
public static class SqlSugarServiceCollectionExtensions
{
    /// <summary>
    /// 服务扩展
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddSqlSugar(this IServiceCollection services)
    {
        //注入配置
        services.AddOptions<List<ConnectionConfig>>()
               .BindConfiguration("ConnectionConfigs");
        SugarContext.InitConnectionConfigs(services.BuildServiceProvider().GetService<IOptions<List<ConnectionConfig>>>().Value);
        //注入仓储
        services.AddScoped(typeof(SugarRepository<>));
        return services;
    }
}
